package com.example.designpattern.delegate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 委托模式如何实现
 * @author long
 * @date 2024/11/16 22:04
 */
//@Service
public class TestDelegateService {

    @Autowired
    private List<ICheckService> checkServiceList;

    /**
     * 策略模式实现方式
     * @param type
     * @return
     */
    public String checkForStrategy(Integer type){
        CheckTypeEnum checkTypeEnum = CheckTypeEnum.getEnumByCode(type);

        // 策略模式选择需要执行的service
        for(ICheckService service : checkServiceList){
            if(Objects.equals(service.checkType(),checkTypeEnum)){
                service.doCheck(type);
            }
        }
        return "success";
    }

    @Autowired
    private ICheckService checkService;

    /**
     * 委托模式具体实现
     * @param type
     * @return
     */
    public String checkForDelegate(Integer type){

        checkService.doCheck(type);

        return "success";
    }

}
